import request from '@/utils/request'

export enum MediaType {
  PHOTO = 'photo',
  VIDEO = 'video'
}

export enum ActivityType {
  ALL = 'all',
  REVIEW = 'review',
  PRODUCT = 'product',
  MEETING = 'meeting',
  INTERVIEW = 'interview'
}

export interface Media {
  id: string
  name: string
  description: string
  type: MediaType
  activityType: ActivityType
  url: string
}

export interface MediaQuery {
  page: number
  size: number
  name?: string
  type?: MediaType
  activityType?: ActivityType
}

export interface JsonData<T = any> {
  code: number
  data: T
  msg: string
}

// 获取媒体列表
export const getMediaList = (params: MediaQuery) => {
  return request.post<any,{
    current_data: Media[]
    total_record: number
  }>('/api/media/list', params)
}

/**
 * 获取媒体详情
 * @param id 媒体ID
 * @returns 媒体详细信息
 */
export const getMediaDetail = (id: string) => {
  return request.get<Media>(`/api/media/${id}`)
}

/**
 * 上传媒体文件
 * @param file 文件
 * @returns 上传后的文件信息
 */
export const uploadMediaFile = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post<string>('/api/media/upload', formData)
}

/**
 * 删除媒体文件
 * @param mediaId 媒体IDstring
 */
export const deleteMediaFile = (mediaId: string) => {
  return request.delete<JsonData>(`/api/media/${mediaId}/file`)
}

// 添加媒体
export const addMedia = (data: Partial<Media>) => {
  return request.post<JsonData>('/api/media/add', data)
}

// 更新媒体
export const updateMedia = (id: string, data: Partial<Media>) => {
  return request.put<JsonData>(`/api/media/${id}`, data)
}

// 删除媒体
export const deleteMedia = (id: string) => {
  return request.delete<JsonData>(`/api/media/${id}`)
}

// 批量删除媒体
export const batchDeleteMedia = (ids: string[]) => {
  return request.post<JsonData>('/api/media/batch', ids)
}

/**
 * 批量下载媒体文件
 * @param ids 媒体ID数组
 * @returns 压缩包文件流
 */
export const batchDownloadMedia = (ids: string[]) => {
  return request.post('/api/media/batch_download', ids, {
    responseType: 'blob'
  })
}